import 'package:azlistview/azlistview.dart';
import 'package:country_state_city/country_state_city.dart';
import 'package:get/get.dart';
import 'package:lpinyin/lpinyin.dart';
import 'package:share_fit/app/controllers/base/base_controller.dart';
import 'package:share_fit/app/controllers/device/country/country_mdel.dart';
import 'package:share_fit/app/controllers/main/login/country/city_model.dart';
import 'package:share_fit/app/helper/ext/tr_ext.dart';
import 'package:share_fit/generated/locales.g.dart';

import 'state.dart';

class CountryLogic extends BaseController {
  final CountryState state = CountryState();

  void getCountriesList() {
    Future(() => getAllCountries().then((value) => _onGetCountriesList(value))
    . then((value) => onCountiesFetchComplete(value)));
  }

  Future<List<CityModel>> _onGetCountriesList(List<Country> countries) {
    List words = countries
        .map((e) => PinyinHelper.getFirstWordPinyin(e.name)
        .substring(0, 1)
        .toUpperCase())
        .toList();
    // words.sort((a, b) => a.compareTo(b));
    final sortedSet = words.toSet();
    state.cityListTemp.clear();
    for (var word in sortedSet) {
      var model = CountryModel(section: word, countries: []);
      for (var countryItem in countries) {
        var temp = PinyinHelper.getShortPinyin(countryItem.name)
            .substring(0, 1)
            .toUpperCase();
        if (word == temp) {
          model.getCountries.add(countryItem);
        }

        CityModel cityModel = CityModel(countryItem.isoCode, name: countryItem.name);
        if (word == temp) {
          state.cityListTemp.add(cityModel);
        }
      }
    }

    SuspensionUtil.sortListBySuspensionTag(state.cityListTemp);
    _handleList(state.cityListTemp);
    return Future(() => state.cityListTemp);
  }

  void onCountiesFetchComplete(List<CityModel> countriesList) {
    state.cityList.clear();
    state.cityList.addAll(countriesList);
    state.isLoading = false;
    update(['country_loading_item']);
  }

  void _handleList(List<CityModel> list) {
    if (list.isEmpty) return;
    for (int i = 0, length = list.length; i < length; i++) {
      String pinyin = PinyinHelper.getPinyinE(list[i].name);
      String tag = pinyin.substring(0, 1).toUpperCase();
      if (RegExp('[A-Z]').hasMatch(tag)) {
        list[i].tagIndex = tag;
      } else {
        list[i].tagIndex = '#';
      }
    }
    // A-Z sort.
    SuspensionUtil.sortListBySuspensionTag(list);

    // add hotCityList.
    state.cityListTemp.insertAll(0, state.hotCityList);

    // show sus tag.
    SuspensionUtil.setShowSuspensionStatus(state.cityList);
  }

  Future<List<CountryModel>> getCountries() {
    return Future(() => getAllCountries().then((value) => _onGetCountries(value)));
        // .then((value) => _onCountiesFetchComplete(value))););
  }

  Future<List<CountryModel>> _onGetCountries(List<Country> countries) {
    List words = countries
        .map((e) => PinyinHelper.getFirstWordPinyin(e.name)
        .substring(0, 1)
        .toUpperCase())
        .toList();
    words.sort((a, b) => a.compareTo(b));
    final sortedSet = words.toSet();
    List<CountryModel> countriesList = [];
    for (var word in sortedSet) {
      var model = CountryModel(section: word, countries: []);
      for (var countryItem in countries) {
        var temp = PinyinHelper.getShortPinyin(countryItem.name)
            .substring(0, 1)
            .toUpperCase();
        if (word == temp) {
          model.getCountries.add(countryItem);
        }
      }
      countriesList.add(model);
    }

    state.countriesList.clear();
    state.countriesList.addAll(countriesList);
    state.isLoading = false;
    return Future(() => countriesList);
  }

  void onCitySelected(CityModel city) {
    var result = {'name': city.name, 'code':city.isoCode};

    updateCountryName(result['name']);
    save('country_selected_mode', result, global: true);
  }

  void onCountrySelected(Country country) {
    var result = {'name': country.name, 'code':country.isoCode};
    Get.back(result: result);
  }

  void updateCountryName(value) {
    state.selectedCountryName = value;
    update(['selected_country_name']);
  }

  @override
  String getRemotePath() {
    return '';
  }

  @override
  void initState() {
    // CityModel hotCity = CityModel('', name: LocaleKeys.country_rec_china.ts);
    // state.hotCityList.add(hotCity);
  }

  @override
  void onRemoteEvent(event, method) {
    // TODO: implement onRemoteEvent
  }


}
